<?php
/**
 * Created by PhpStorm.
 * User: huangshuaihui
 * Date: 2017/6/7
 * Time: 下午4:56
 */

namespace app\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Model\Admin\ImportDsp;
use App\Http\Model\Admin\Tourist;
use App\Http\Model\Admin\TouristAction;
use App\Http\Model\Admin\TouristInfo;
use Webpatser\Uuid\Uuid;

class ImportDspController extends BaseController
{
    public function index(){
        $n = new ImportDsp();
        $returnArray = $n->getDspData();
//        var_dump($returnArray);
        if(count($returnArray)) {
            foreach ($returnArray as $result) {
                $tourist = new Tourist;


                $tourist->city = $result['city'];
                $tourist->source_id = $result['source_id'];
                $tourist->phone = $result['phone'];
                $tourist_rs = $tourist->save();

                $tourist_info = new TouristInfo;
                $info = $tourist_info->findOneByPhone($result['phone']);

                if (count($info) == 0 || !$info) {
                    $tourist_info = new TouristInfo;
                    $tourist_info->phone = $result['phone'];
                    $tourist_info->price = isset($result['house_price']) ? $result['house_price'] : 0;
                    $tourist_info->area = isset($result['house_totalarea']) ? $result['house_totalarea'] : 0;
                    $tourist_info->cityarea_id = $result['cityarea_id'];
                    $tourist_info->cityarea_name = $result['cityarea_name'];
                    $tourist_info->house_room = isset($result['house_room']);
                    $tourist_info->house_hall = isset($result['house_hall']) ? $result['house_hall'] : 0;
                    $tourist_info->house_toilet = isset($result['house_toilet']) ? $result['house_toilet'] : 0;
                    $tourist_info->city = $result['city'];
                    $tourist_info->appName = isset($result['appName']) ? $result['appName'] : '';
                    $tourist_info->save();
                    
                }

                $action_ins = new TouristAction();
                $action_ins->setTable('tourists_actions' . $this->getTableNum($result['phone']));
                $result['id'] = Uuid::generate()->string;
                $result['user_id'] = $tourist['id'];
                $action_ins->createBatch($result); 
            }
        }
    }
}